﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using libConn;

namespace ApartmentSale
{
    public partial class InvoiceEdit : Window
    {
        public InvoiceEdit()
        {
            InitializeComponent();
            invoice = new InvoiceView();
            opCommand = OpCommand.Create;
        }

        public InvoiceEdit(int apartment_id, double invoice_amount, string custom_name)
            :this()
        {
            invoice.apartment_id = apartment_id;
            invoice.invoice_amount = invoice_amount;
            invoice.invoice_custom_name = custom_name;

            cbStatus.IsEnabled = false;
            this.DataContext = invoice;
        }

        public InvoiceEdit(InvoiceView _invoice)
            :this()
        {
            opCommand = OpCommand.Update;
            invoice = _invoice;
            tbCode.IsEnabled = false;

            this.DataContext = invoice;
        }

        private enum OpCommand { Create, Update }
        private OpCommand opCommand;

        private InvoiceView invoice;

        private bool checkInvoiceCode(string data)
        {
            string errMessage = null;
            string str = "SELECT * FROM invoice WHERE invoice_code = '"
                + data.Replace("'", "''") + "'";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Check the invoice SN failed",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            if (oleDT.Rows.Count > 0)
            {
                System.Windows.MessageBox.Show(
                    "Exist in the same invoice SN, please check.",
                    "Save failed",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Stop);
                return false;
            }

            return true;
        }

        private void btOK_Click(object sender, RoutedEventArgs e)
        {
            if (tbCode.Text == "")
            {
                MessageBox.Show("Please enter the invoice SN.");
                tbCode.Focus();
                return;
            }

            double d;
            if (tbAmount.Text == "" || !Double.TryParse(tbAmount.Text, out d) || d <= 0)
            {
                MessageBox.Show("Please enter a valid amount, effective financial must be greater than 0.");
                tbAmount.Focus();
                return;
            }

            if (dpDate.SelectedDate == null)
            {
                MessageBox.Show("Please enter the billing date.");
                dpDate.Focus();
                return;
            }

            string errMessage = "";

            if (opCommand == OpCommand.Create)
            {
                if (!checkInvoiceCode(tbCode.Text))
                {
                    tbCode.Focus();
                    return;
                }
                errMessage = invoice.Insert();
            }
            else
            {
                errMessage = invoice.Update();
            }

            if (errMessage != "")
            {
                MessageBox.Show(errMessage, "Save failed", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            this.Close();
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
